package com.kurumi.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.config.MyConfig;
import com.kurumi.pojo.MedicalRecord;
import com.kurumi.pojo.RespondResult;
import com.kurumi.pojo.coding.BasicInfo;
import com.kurumi.pojo.coding.CostInfo;
import com.kurumi.pojo.coding.CureInfo;
import com.kurumi.pojo.coding.DiseaseDiagInfo;
import com.kurumi.pojo.coding.DiseaseDiagRecord;
import com.kurumi.pojo.coding.DrugAllergyInfo;
import com.kurumi.pojo.coding.InfectionInfo;
import com.kurumi.pojo.coding.InfusionBloodInfo;
import com.kurumi.pojo.coding.IntensiveCareInfo;
import com.kurumi.pojo.coding.NurseInfo;
import com.kurumi.pojo.coding.OperateInfo;
import com.kurumi.pojo.coding.TransferDeptInfo;
import com.kurumi.pojo.coding_attached.DRGsManageInfo;
import com.kurumi.pojo.coding_attached.HospitalSurveyInfectionInfo;
import com.kurumi.pojo.coding_attached.MedicalQualityControlInfo;
import com.kurumi.pojo.coding_attached.NurseQualityControlInfo;
import com.kurumi.query.MedicalRecordQuery;
import com.kurumi.query.StatisticsAnalysisQuery;
import com.kurumi.service.BaseInfoService;
import com.kurumi.service.MedicalRecordCodingService;
import com.kurumi.service.MedicalRecordService;
import com.kurumi.util.DateUtil;
import com.kurumi.util.FileUtil;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.PDFUtil;
import com.kurumi.util.StringUtil;
import com.kurumi.util.WaterMarkUtil;

@Controller
@RequestMapping("/medical_record_attached_coding")
public class MedicalRecordAttachedCodingController {

	@Autowired
	private MyConfig myConfig;
	
	@Autowired
	private BaseInfoService baseInfoService;
	
	@Autowired
	private MedicalRecordService medicalRecordService;
	
	@Autowired
	private MedicalRecordCodingService medicalRecordCodingService;
	
	
	@GetMapping("/edit_index_info")
	public String editHomePageIndexForm(String visitGuid,Model model){
		model.addAttribute("visitGuid", visitGuid);
		List<Map<String,Object>> medicalRecords = medicalRecordService.getMedicalRecordByVisitGuid(visitGuid);
		if(medicalRecords.size() == 0){
			RespondResult respondResult = new RespondResult(false,"404","网址错误，资源不存在",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}else{
			Map<String,Object> medicalRecord = medicalRecords.get(0);
			RespondResult respondResult = new RespondResult(true,"302","",medicalRecord);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		return "coding_attached/edit_index_info";
	}
	
	@GetMapping("/edit_medical_quality_info_form")
	public String editMedicalQualityInfoForm(String visitGuid,Model model){
		MedicalRecord medicalRecord = medicalRecordService.selectMedicalRecordByPrimaryKey(visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		Map<String, Object> medicalQualityControlInfo = null;
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			medicalQualityControlInfo = (Map<String, Object>)jsonMap.get("medicalQualityControlInfo");
		}
		String medicalQualityControlInfoJson = null;
		if(medicalQualityControlInfo == null){
			medicalQualityControlInfo = new HashMap<String, Object>();
			medicalQualityControlInfoJson = JsonUtil.objectToJson(new MedicalQualityControlInfo());
		}else{
			medicalQualityControlInfoJson = JsonUtil.objectToJson(medicalQualityControlInfo);
		}
		String medicalRecordJson = JsonUtil.objectToJson(medicalRecord);
		model.addAttribute("medicalRecordJson", medicalRecordJson);
		
		model.addAttribute("medicalQualityControlInfoJson", medicalQualityControlInfoJson);
		model.addAttribute("visitGuid", visitGuid);
		return "coding_attached/edit_medical_quality_info";
	}
	
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_medical_quality_info")
	public String editMedicalQualityControlInfo(MedicalQualityControlInfo medicalQualityControlInfo,HttpServletRequest request,Model model){
		
		try {
			
			String visitGuid = medicalQualityControlInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = visitGuid + ".json";
			String versionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("medicalQualityControlInfo", medicalQualityControlInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editMedicalQualityControlInfo(visitGuid, medicalQualityControlInfo, jsonMap);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", visitGuid);
			Map<String, Object> medicalQualityControlInfoMap = (Map<String, Object>)jsonMap.get("medicalQualityControlInfo");
			String medicalQualityControlInfoJson = JsonUtil.objectToJson(medicalQualityControlInfoMap);
			model.addAttribute("medicalQualityControlInfoJson", medicalQualityControlInfoJson);
			model.addAttribute("medicalQualityControlInfo",  medicalQualityControlInfoMap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		
		return "coding_attached/edit_medical_quality_info";
	}
	
	
	@GetMapping("/edit_nurse_quality_info_form")
	public String editNurseQualityInfoForm(String visitGuid,Model model){
		MedicalRecord medicalRecord = medicalRecordService.selectMedicalRecordByPrimaryKey(visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		Map<String, Object> nurseQualityControlInfo = null;
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			nurseQualityControlInfo = (Map<String, Object>)jsonMap.get("nurseQualityControlInfo");
		}
		String nurseQualityControlInfoJson = null;
		if(nurseQualityControlInfo == null){
			nurseQualityControlInfo = new HashMap<String, Object>();
			nurseQualityControlInfoJson = JsonUtil.objectToJson(new NurseQualityControlInfo());
		}else{
			nurseQualityControlInfoJson = JsonUtil.objectToJson(nurseQualityControlInfo);
		}
		String medicalRecordJson = JsonUtil.objectToJson(medicalRecord);
		model.addAttribute("medicalRecordJson", medicalRecordJson);
		
		model.addAttribute("nurseQualityControlInfoJson", nurseQualityControlInfoJson);
		model.addAttribute("visitGuid", visitGuid);
		return "coding_attached/edit_nurse_quality_info";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_nurse_quality_info")
	public String editNurseQualityControlInfo(NurseQualityControlInfo nurseQualityControlInfo,HttpServletRequest request,Model model){
		
		try {
			
			String visitGuid = nurseQualityControlInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = visitGuid + ".json";
			String versionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("nurseQualityControlInfo", nurseQualityControlInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editNurseQualityControlInfo(visitGuid, nurseQualityControlInfo, jsonMap);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", visitGuid);
			Map<String, Object> nurseQualityControlInfoMap = (Map<String, Object>)jsonMap.get("nurseQualityControlInfo");
			String nurseQualityControlInfoJson = JsonUtil.objectToJson(nurseQualityControlInfoMap);
			model.addAttribute("nurseQualityControlInfoJson", nurseQualityControlInfoJson);
			model.addAttribute("nurseQualityControlInfo",  nurseQualityControlInfoMap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		
		return "coding_attached/edit_nurse_quality_info";
	}
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("/edit_hospital_survey_infection_info_form")
	public String editHospitalSurveyInectionInfoForm(String visitGuid,Model model){
		MedicalRecord medicalRecord = medicalRecordService.selectMedicalRecordByPrimaryKey(visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		Map<String, Object> hospitalSurveyInfectionInfo = null;
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			hospitalSurveyInfectionInfo = (Map<String, Object>)jsonMap.get("hospitalSurveyInfectionInfo");
		}
		String hospitalSurveyInfectionInfoJson = null;
		if(hospitalSurveyInfectionInfo == null){
			hospitalSurveyInfectionInfo = new HashMap<String, Object>();
			hospitalSurveyInfectionInfoJson = JsonUtil.objectToJson(new HospitalSurveyInfectionInfo());
		}else{
			 hospitalSurveyInfectionInfoJson = JsonUtil.objectToJson(hospitalSurveyInfectionInfo);
		}
		String medicalRecordJson = JsonUtil.objectToJson(medicalRecord);
		model.addAttribute("medicalRecordJson", medicalRecordJson);
		
		model.addAttribute("hospitalSurveyInfectionInfoJson", hospitalSurveyInfectionInfoJson);
		model.addAttribute("visitGuid", visitGuid);
		return "coding_attached/edit_hospital_survey_infection_info";
	}
	
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_hospital_survey_infection_info")
	public String editHospitalSurveyInectionInfo(HospitalSurveyInfectionInfo hospitalSurveyInfectionInfo,HttpServletRequest request,Model model){
		
		try {
			
			String visitGuid = hospitalSurveyInfectionInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = visitGuid + ".json";
			String versionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("hospitalSurveyInfectionInfo", hospitalSurveyInfectionInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editHospitalSurveyInfectionInfo(visitGuid, hospitalSurveyInfectionInfo, jsonMap);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", visitGuid);
			Map<String, Object> hospitalSurveyInfectionInfoMap = (Map<String, Object>)jsonMap.get("hospitalSurveyInfectionInfo");
			String hospitalSurveyInfectionInfoJson = JsonUtil.objectToJson(hospitalSurveyInfectionInfoMap);
			model.addAttribute("hospitalSurveyInfectionInfoJson", hospitalSurveyInfectionInfoJson);
			model.addAttribute("hospitalSurveyInfectionInfo",  hospitalSurveyInfectionInfoMap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		
		return "coding_attached/edit_hospital_survey_infection_info";
	}
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("/edit_drgs_manage_info_form")
	public String editDRGsManageInfoForm(String visitGuid,Model model){
		MedicalRecord medicalRecord = medicalRecordService.selectMedicalRecordByPrimaryKey(visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		Map<String, Object> drgsManageInfo = null;
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			drgsManageInfo = (Map<String, Object>)jsonMap.get("drgsManageInfo");
		}
		if(drgsManageInfo == null){
			drgsManageInfo = new HashMap<String, Object>();
		}
		String medicalRecordJson = JsonUtil.objectToJson(medicalRecord);
		model.addAttribute("medicalRecordJson", medicalRecordJson);
		String drgsManageInfoJson = JsonUtil.objectToJson(drgsManageInfo);
		model.addAttribute("drgsManageInfoJson", drgsManageInfoJson);
		model.addAttribute("visitGuid", visitGuid);
		return "coding_attached/edit_drgs_manage_info";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_drgs_manage_info")
	public String editDRGsManageInfo(DRGsManageInfo drgsManageInfo,HttpServletRequest request,Model model){
		try {
			
			String visitGuid = drgsManageInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = visitGuid + ".json";
			String versionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("drgsManageInfo", drgsManageInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editDRGsManageInfoInfo(visitGuid, drgsManageInfo, jsonMap);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", visitGuid);
			Map<String, Object> drgsManageInfoMap = (Map<String, Object>)jsonMap.get("drgsManageInfo");
			String drgsManageInfoJson = JsonUtil.objectToJson(drgsManageInfoMap);
			model.addAttribute("drgsManageInfoJson", drgsManageInfoJson);
			model.addAttribute("drgsManageInfoInfo",  drgsManageInfoMap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		
		return "coding_attached/edit_drgs_manage_info";
	}
	
	
	@GetMapping("/edit_infusion_blood_info_form")
	public String editInfusionBloodInfoForm(String visitGuid,Model model){
		
		model.addAttribute("visitGuid", visitGuid);
		Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseDataOfInfusionBloodInfo();
		String baseInfoJson = JsonUtil.objectToJson(baseInfo);
		model.addAttribute("baseInfoJson", baseInfoJson);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		
		Map<String, Object> infusionBloodInfo = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			infusionBloodInfo = (Map<String, Object>)jsonMap.get("infusionBloodInfo");
			if(infusionBloodInfo == null){
				infusionBloodInfo = new HashMap<String, Object>();
			}
		}
		
		String infusionBloodInfoJson = JsonUtil.objectToJson(infusionBloodInfo);
		model.addAttribute("infusionBloodInfo", infusionBloodInfo);
		model.addAttribute("infusionBloodInfoJson", infusionBloodInfoJson);
		return "coding/edit_infusion_blood_info";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_infusion_blood_info")
	public String editInfusionBloodInfo(InfusionBloodInfo infusionBloodInfo,HttpServletRequest request,Model model){
		try {
			String visitGuid = infusionBloodInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = infusionBloodInfo.getVisitGuid() + ".json";
			String versionFileName = infusionBloodInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(infusionBloodInfo.getVisitGuid());
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("infusionBloodInfo", infusionBloodInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editInfusionBloodInfo(infusionBloodInfo.getVisitGuid(), infusionBloodInfo, jsonMap);
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", infusionBloodInfo.getVisitGuid());
			
			Map<String, Object> infusionBloodInfoMap = (Map<String, Object>)jsonMap.get("infusionBloodInfo");
			String infusionBloodInfoJson = JsonUtil.objectToJson(infusionBloodInfoMap);
			model.addAttribute("infusionBloodInfo", infusionBloodInfoMap);
			
			model.addAttribute("infusionBloodInfoJson",infusionBloodInfoJson);
			Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseDataOfInfusionBloodInfo();
			String baseInfoJson = JsonUtil.objectToJson(baseInfo);
			model.addAttribute("baseInfoJson", baseInfoJson);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		return "coding/edit_infusion_blood_info";
	}
	
	@GetMapping("/edit_transfer_dept_info_form")
	public String editTransferDeptInfoForm(String visitGuid,Model model){
		
		model.addAttribute("visitGuid", visitGuid);
		Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseInfoOfTransferDeptInfo();
		String baseInfoJson = JsonUtil.objectToJson(baseInfo);
		model.addAttribute("baseInfoJson", baseInfoJson);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		
		Map<String, Object> transferDeptInfo = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			transferDeptInfo = (Map<String, Object>)jsonMap.get("transferDeptInfo");
			if(transferDeptInfo == null){
				transferDeptInfo = new HashMap<String, Object>();
			}
		}
		
		String transferDeptInfoJson = JsonUtil.objectToJson(transferDeptInfo);
		model.addAttribute("transferDeptInfo", transferDeptInfo);
		model.addAttribute("transferDeptInfoJson", transferDeptInfoJson);
		return "coding/edit_transfer_dept_info";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_transfer_dept_info")
	public String editTransferDeptInfoForm(TransferDeptInfo transferDeptInfo,HttpServletRequest request,Model model){
		try {
			
			String visitGuid = transferDeptInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = transferDeptInfo.getVisitGuid() + ".json";
			String versionFileName = transferDeptInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			transferDeptInfo.clearEmpty();
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(transferDeptInfo.getVisitGuid());
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("transferDeptInfo", transferDeptInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editTransferDeptInfo(transferDeptInfo.getVisitGuid(), transferDeptInfo, jsonMap);
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", transferDeptInfo.getVisitGuid());
			
			Map<String, Object> transferDeptInfoMap = (Map<String, Object>)jsonMap.get("transferDeptInfo");
			String transferDeptInfoJson = JsonUtil.objectToJson(transferDeptInfoMap);
			model.addAttribute("transferDeptInfo", transferDeptInfoMap);
			
			model.addAttribute("transferDeptInfoJson",transferDeptInfoJson);
			Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseInfoOfTransferDeptInfo();
			String baseInfoJson = JsonUtil.objectToJson(baseInfo);
			model.addAttribute("baseInfoJson", baseInfoJson);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		return "coding/edit_transfer_dept_info";
	}
	
	@GetMapping("/edit_drug_allergy_info_form")
	public String editDrugAllergyInfoForm(String visitGuid,Model model){
		
		model.addAttribute("visitGuid", visitGuid);
		Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseDataOfDrugAllergyInfo();
		String baseInfoJson = JsonUtil.objectToJson(baseInfo);
		model.addAttribute("baseInfoJson", baseInfoJson);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		
		Map<String, Object> drugAllergyInfo = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			drugAllergyInfo = (Map<String, Object>)jsonMap.get("drugAllergyInfo");
			if(drugAllergyInfo == null){
				drugAllergyInfo = new HashMap<String, Object>();
			}
		}
		
		String drugAllergyInfoJson = JsonUtil.objectToJson(drugAllergyInfo);
		model.addAttribute("drugAllergyInfo", drugAllergyInfo);
		model.addAttribute("drugAllergyInfoJson",drugAllergyInfoJson);
		return "coding/edit_drug_allergy_info";
	}
	
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_drug_allergy_info")
	public String editDrugAllergyInfo(DrugAllergyInfo drugAllergyInfo,HttpServletRequest request,Model model){
		try {
			
			String visitGuid = drugAllergyInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = drugAllergyInfo.getVisitGuid() + ".json";
			String versionFileName = drugAllergyInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(drugAllergyInfo.getVisitGuid());
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("drugAllergyInfo", drugAllergyInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editDrugAllergyInfo(drugAllergyInfo.getVisitGuid(), drugAllergyInfo, jsonMap);
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", drugAllergyInfo.getVisitGuid());
			
			Map<String, Object> drugAllergyInfoMap = (Map<String, Object>)jsonMap.get("drugAllergyInfo");
			String drugAllergyInfoJson = JsonUtil.objectToJson(drugAllergyInfoMap);
			model.addAttribute("drugAllergyInfo",drugAllergyInfoMap);
			
			model.addAttribute("drugAllergyInfoJson",drugAllergyInfoJson);
			Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseDataOfDrugAllergyInfo();
			String baseInfoJson = JsonUtil.objectToJson(baseInfo);
			model.addAttribute("baseInfoJson", baseInfoJson);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		
		return "coding/edit_drug_allergy_info";
	}
	
	
	@GetMapping("/edit_nurse_info_form")
	public String editNurseInfoForm(String visitGuid,Model model){
		model.addAttribute("visitGuid", visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		
		Map<String, Object> nurseInfo = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			nurseInfo = (Map<String, Object>)jsonMap.get("nurseInfo");
			if(nurseInfo == null){
				nurseInfo = new HashMap<String, Object>();
			}
		}
		
		String nurseInfoJson = JsonUtil.objectToJson(nurseInfo);
		model.addAttribute("nurseInfo", nurseInfo);
		model.addAttribute("nurseInfoJson", nurseInfoJson);
		return "coding/edit_nurse_info";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_nurse_info")
	public String editNurseInfo(NurseInfo nurseInfo,HttpServletRequest request,Model model){
		try {
			
			String visitGuid = nurseInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = nurseInfo.getVisitGuid() + ".json";
			String versionFileName = nurseInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(nurseInfo.getVisitGuid());
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
			jsonMap.put("userCode", userCode);
			jsonMap.put("userName", userName);
			jsonMap.put("nurseInfo", nurseInfo);
			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
			medicalRecordCodingService.editNurseInfo(nurseInfo.getVisitGuid(), nurseInfo, jsonMap);
			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
			model.addAttribute("visitGuid", nurseInfo.getVisitGuid());
			
			Map<String, Object> nurseInfoMap = (Map<String, Object>)jsonMap.get("nurseInfo");
			String nurseInfoJson = JsonUtil.objectToJson(nurseInfoMap);
			model.addAttribute("nurseInfo", nurseInfoMap);
			
			model.addAttribute("nurseInfoJson",nurseInfoJson);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		
		return "coding/edit_nurse_info";
	}
	
	@GetMapping("/edit_intensive_care_info_form")
	public String editIntensiveCareInfoForm(String visitGuid,Model model){
		Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseDataOfIntensiveCareInfo();
		String baseInfoJson = JsonUtil.objectToJson(baseInfo);
		model.addAttribute("baseInfoJson", baseInfoJson);
		model.addAttribute("visitGuid", visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		
		Map<String, Object> intensiveCareInfo = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			intensiveCareInfo = (Map<String, Object>)jsonMap.get("intensiveCareInfo");
			if(intensiveCareInfo == null){
				intensiveCareInfo = new HashMap<String, Object>();
			}
		}
		
		String intensiveCareInfoJson = JsonUtil.objectToJson(intensiveCareInfo);
		model.addAttribute("intensiveCareInfo", intensiveCareInfo);
		model.addAttribute("intensiveCareInfoJson", intensiveCareInfoJson);
		return "coding/edit_intensive_care_info";
	}
	
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_intensive_care_info")
	public String editIntensiveCareInfo(IntensiveCareInfo intensiveCareInfo,HttpServletRequest request,Model model){
		try {
			
			String visitGuid = intensiveCareInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = intensiveCareInfo.getVisitGuid() + ".json";
			String versionFileName = intensiveCareInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(intensiveCareInfo.getVisitGuid());
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
 				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
 			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
 			jsonMap.put("userCode", userCode);
 			jsonMap.put("userName", userName);
 			jsonMap.put("intensiveCareInfo", intensiveCareInfo);
 			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
 			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
 			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
  			medicalRecordCodingService.editIntensiveCareInfo(intensiveCareInfo.getVisitGuid(), intensiveCareInfo, jsonMap);
  			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
 			model.addAttribute("visitGuid", intensiveCareInfo.getVisitGuid());
			
 			Map<String, Object> intensiveCareInfoMap = (Map<String, Object>)jsonMap.get("intensiveCareInfo");
			String intensiveCareInfoJson = JsonUtil.objectToJson(intensiveCareInfoMap);
			model.addAttribute("intensiveCareInfo", intensiveCareInfoMap);
			
			model.addAttribute("intensiveCareInfoJson",intensiveCareInfoJson);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			Map<String, List<Map<String, Object>>> baseInfo = baseInfoService.getBaseDataOfIntensiveCareInfo();
			String baseInfoJson = JsonUtil.objectToJson(baseInfo);
			model.addAttribute("baseInfoJson", baseInfoJson);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		return "coding/edit_intensive_care_info";
	}
	
	@GetMapping("/edit_infection_info_form")
	public String editInfectionInfoForm(String visitGuid,Model model){
		model.addAttribute("visitGuid", visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		
		Map<String, Object> infectionInfo = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			infectionInfo = (Map<String, Object>)jsonMap.get("infectionInfo");
			if(infectionInfo == null){
				infectionInfo = new HashMap<String, Object>();
			}
		}
		
		String infectionInfoJson = JsonUtil.objectToJson(infectionInfo);
		model.addAttribute("infectionInfo", infectionInfo);
		model.addAttribute("infectionInfoJson", infectionInfoJson);
		return "coding/edit_infection_info";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_infection_info")
	public String editInfectionInfo(InfectionInfo infectionInfo,HttpServletRequest request,Model model){
		try {
			
			String visitGuid = infectionInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = infectionInfo.getVisitGuid() + ".json";
			String versionFileName = infectionInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(infectionInfo.getVisitGuid());
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
 				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
 			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
 			jsonMap.put("userCode", userCode);
 			jsonMap.put("userName", userName);
 			jsonMap.put("infectionInfo", infectionInfo);
 			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
 			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
 			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
  			medicalRecordCodingService.editInfectionInfo(infectionInfo.getVisitGuid(), infectionInfo, jsonMap);
  			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
 			model.addAttribute("visitGuid", infectionInfo.getVisitGuid());
			
 			Map<String, Object> infectionInfoMap = (Map<String, Object>)jsonMap.get("infectionInfo");
			String infectionInfoJson = JsonUtil.objectToJson(infectionInfoMap);
			model.addAttribute("infectionInfo", infectionInfoMap);
			
			model.addAttribute("infectionInfoJson",infectionInfoJson);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		return "coding/edit_infection_info";
	}
	
	@GetMapping("/edit_cost_info_form")
	public String editCostInfoForm(String visitGuid,Model model){
		model.addAttribute("visitGuid", visitGuid);
		List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
		
		Map<String, Object> costInfo = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			Map<String, Object> jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
			costInfo = (Map<String, Object>)jsonMap.get("costInfo");
			if(costInfo == null){
				costInfo = new HashMap<String, Object>();
			}
		}
		
		String costInfoJson = JsonUtil.objectToJson(costInfo);
		model.addAttribute("costInfo", costInfo);
		model.addAttribute("costInfoJson", costInfoJson);
		return "coding/edit_cost_info";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("/edit_cost_info")
	public String editCostInfo(CostInfo costInfo,HttpServletRequest request,Model model){
		try {
			
			String visitGuid = costInfo.getVisitGuid();
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
			String versionFilePath =filePath+"version\\";
			String fileName = costInfo.getVisitGuid() + ".json";
			String versionFileName = costInfo.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
			
			/*String jsonData = FileUtil.readFile(filePath, fileName);*/
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(costInfo.getVisitGuid());
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
 				if(jsonMap == null){
					jsonMap = new HashMap<String, Object>();
				}
			}
 			Subject subject=SecurityUtils.getSubject();
			Session session = subject.getSession();
			Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
			String userCode =(String)currentUser.get("user_code");
			String userName =(String)currentUser.get("user_name");
 			jsonMap.put("userCode", userCode);
 			jsonMap.put("userName", userName);
 			jsonMap.put("costInfo", costInfo);
 			String jsonMapJson = JsonUtil.objectToJson(jsonMap);
 			FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
 			FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
  			medicalRecordCodingService.editCostInfo(costInfo.getVisitGuid(), costInfo, jsonMap);
  			jsonMap = JsonUtil.jsonToPojo(jsonMapJson, Map.class);
 			model.addAttribute("visitGuid", costInfo.getVisitGuid());
			
 			Map<String, Object> costInfoMap = (Map<String, Object>)jsonMap.get("costInfo");
			String costInfoJson = JsonUtil.objectToJson(costInfoMap);
			model.addAttribute("costInfo", costInfoMap);
			
			model.addAttribute("costInfoJson",costInfoJson);
			RespondResult respondResult = new RespondResult(true,"200","保存成功",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			RespondResult respondResult = new RespondResult(false,"500","保存失败",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		return "coding/edit_cost_info";
	}
	
	
	@PostMapping("/ajax_coding_finish")
	@ResponseBody
	public RespondResult ajaxCodingFinish(String visitGuid){
		RespondResult respondResult = null;
		String errorMessage = null;
		try{
			
			errorMessage = medicalRecordCodingService.codingFinish(visitGuid);
			if(errorMessage == null){
				respondResult = new RespondResult(true, RespondResult.successCode, null, null);
			}else{
				respondResult = new RespondResult(true, RespondResult.errorCode, errorMessage, errorMessage);
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), "编码完成失败");
		}
		
		return respondResult;
	}
	
	@GetMapping("/show_home_page_index")
	public void homePagePrint(String visitGuid, Model model, HttpServletRequest request,HttpServletResponse response) {
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
		ByteArrayOutputStream baos = null;
		try {
			List<String> jsonDatas = medicalRecordCodingService.getMedicalRecordJsonByVisitGuid(visitGuid);
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			Map<String, Object> basicInfo = new HashMap<String, Object>();
			if(!jsonDatas.isEmpty()){
				jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				basicInfo = (Map<String, Object>)jsonMap.get("basicInfo");
			}
			
			if(basicInfo == null){

				basicInfo = new HashMap<String, Object>();
				MedicalRecord medicalRecord = medicalRecordService.selectMedicalRecordByPrimaryKey(visitGuid);
				
				String patientName = medicalRecord.getPatientName();
				basicInfo.put("patientName",patientName);
				
				String sexCode = medicalRecord.getSexCode();
				basicInfo.put("sexCode",sexCode);
				
				String sexName = medicalRecord.getSexName();
				basicInfo.put("sexName",sexName);
				
				Date birthday = medicalRecord.getBirthday();
				if(birthday != null){
					basicInfo.put("birthday", DateUtil.dateFormat(birthday));
				}
				String documentTypeCode = medicalRecord.getDocumentTypeCode();
				basicInfo.put("documentTypeCode",documentTypeCode);
				
				String documentTypeName = medicalRecord.getDocumentTypeName();
				basicInfo.put("documentTypeName",documentTypeName);
				
				String idNumber = medicalRecord.getIdNumber();
				basicInfo.put("idNumber",idNumber);
				
				
				String inDeptCode = medicalRecord.getInDeptCode();
				basicInfo.put("inDeptCode",inDeptCode);
				
				String inDeptName = medicalRecord.getInDeptName();
				basicInfo.put("inDeptName",inDeptName);
				
				Date inHospitalDateTime = medicalRecord.getInHospitalDateTime();
				if(inHospitalDateTime != null){
					basicInfo.put("inHospitalDateTime", DateUtil.dateFormat(DateUtil.DATE_TIME_FORMATE,inHospitalDateTime));
				}
				
				Date outHospitalDateTime = medicalRecord.getOutHospitalDateTime();
				if(outHospitalDateTime != null){
					basicInfo.put("outHospitalDateTime", DateUtil.dateFormat(DateUtil.DATE_TIME_FORMATE,outHospitalDateTime));
				}
				String outDeptCode = medicalRecord.getOutDeptCode();
				basicInfo.put("outDeptCode",outDeptCode);
				
				String outDeptName = medicalRecord.getOutDeptName();
				basicInfo.put("outDeptName",outDeptName);
				
				String outHospitalTypeCode = medicalRecord.getOutHospitalTypeCode();
				basicInfo.put("outHospitalTypeCode",outHospitalTypeCode);
				
				String outHospitalTypeName = medicalRecord.getOutHospitalTypeName();
				basicInfo.put("outHospitalTypeName",outHospitalTypeName);
				
				String mrId = medicalRecord.getMrId();
				basicInfo.put("mrId",mrId);
				
				String onlyId = medicalRecord.getOnlyId();
				basicInfo.put("onlyId",onlyId);
				
				Integer visitNumber = medicalRecord.getVisitNumber();
				basicInfo.put("visitNumber",visitNumber);
			
				jsonMap.put("basicInfo", basicInfo);
			}
			
			List<String> medicalWorkerCodes = new ArrayList<String>();
			Map<String, Object> cureInfo = (Map<String, Object>) jsonMap.get("cureInfo");
			if(cureInfo != null){
				List<Map<String, Object>> medicalWorkers = (List<Map<String, Object>>)cureInfo.get("cureWorkers");
				if(medicalWorkers != null){
					for (Map<String, Object> medicalWorker : medicalWorkers) {
						String medicalWorkerCode = StringUtil.meaningStr((String)medicalWorker.get("medicalWorkerCode"));
						if(medicalWorkerCode != null){
							medicalWorkerCodes.add(medicalWorkerCode);
						}
						
					}
				}
				
			}
			Map<String, Object> signatureMedicalWorks = new HashMap<String, Object>();
			if(!medicalWorkerCodes.isEmpty()){
				signatureMedicalWorks = baseInfoService.getSignatureMedicalWorks(medicalWorkerCodes);
			}
			baos = PDFUtil.getPDFStreamByTemplate(jsonMap, myConfig.getPageIndexpPdfTemplatePath(),signatureMedicalWorks);
			ByteArrayOutputStream out = WaterMarkUtil.getOutputStreamOfWterMarkByText(baos, (String)currentUser.get("user_code"));
			/*ByteArrayOutputStream out = WaterMarkUtil.getOutputStreamOfWterMarkByIcon(baos, "D:\\publics\\medical_record\\water_icon\\305logo.jpg");*/
			response.setContentLength(out.size());
			response.setContentType("application/pdf");
			response.addHeader("Content-Disposition", "inline;FileName=out.pdf");
			
			OutputStream outStream = response.getOutputStream();  
	        outStream.write(out.toByteArray(), 0, out.size());  
	        outStream.flush(); 
	        outStream.close(); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@GetMapping("/index_check_page")
	public String indexCheckPage(Model model){
		return "check/index_check_page";
	}
	
	@GetMapping("/main_disease_diag_check_page")
	public String mainDiseaseDiagCheck(Model model){
		return "check/main_disease_diag_check_page";
	}
	
	
	@GetMapping("/ajax_main_disease_diag_check")
	@ResponseBody
	public RespondResult ajaxMainDiseaseDiagCheck(MedicalRecordQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			if(!params.queryUnEncodingEmpty()){
				medicalRecords = medicalRecordCodingService.mainDiseaseDiagCheck(params);
			}
			
			respondResult = new RespondResult(true, RespondResult.successCode, null, medicalRecords);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), "");
		}
		
		return respondResult;
	}
	
	@GetMapping("/in_hospital_day_number_check_page")
	public String inHospitalDayNumberCheck(Model model){
		return "check/in_hospital_day_number_check_page";
	}
	
	@GetMapping("/in_hospital_day_number_check")
	@ResponseBody
	public RespondResult ajaxInHospitalDayNumberCheck(MedicalRecordQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			if(!params.queryUnEncodingEmpty()){
				medicalRecords = medicalRecordCodingService.inHospitalDayNumberCheck(params);
			}
			
			respondResult = new RespondResult(true, RespondResult.successCode, null, medicalRecords);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), "");
		}
		
		return respondResult;
	}
	
	@GetMapping("/repeat_coding_check_page")
	public String repeatCodingCheckCheck(Model model){
		return "check/repeat_coding_check_page";
	}
	
	@GetMapping("/ajax_repeat_coding_check")
	@ResponseBody
	public RespondResult ajaxRepeatCodingCheckCheck(MedicalRecordQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			if(!params.queryUnEncodingEmpty()){
				medicalRecords = medicalRecordCodingService.repeatCodingCheck(params);
			}
			
			respondResult = new RespondResult(true, RespondResult.successCode, null, medicalRecords);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), "");
		}
		
		return respondResult;
	}
	
	@GetMapping("/date_time_check_page")
	public String dateTimeCheckCheck(Model model){
		return "check/date_time_check_page";
	}
	
	
	@GetMapping("/ajax_date_time_check")
	@ResponseBody
	public RespondResult ajaxDateTimeCheckCheck(MedicalRecordQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			if(!params.queryUnEncodingEmpty()){
				medicalRecords = medicalRecordCodingService.dateTimeCheck(params);
			}
			
			respondResult = new RespondResult(true, RespondResult.successCode, null, medicalRecords);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), "");
		}
		
		return respondResult;
	}
	
	@GetMapping("/medical_record_repair_page")
	public String medicalRecordRepairPage(){
		return "coding/medical_record_repair_page";
	}
	
	
	@GetMapping("/query_repair_medical_record")
	@ResponseBody
	public RespondResult queryRepairMedicalRecord(MedicalRecordQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			int count = 0;
			if(!params.queryUnEncodingEmpty()){
				medicalRecords = medicalRecordCodingService.getMedicalRecordOfRepair(params);
				count = medicalRecordCodingService.getMedicalRecordCountOfRepair(params);
			}
			
			params.setTotalCounts(count);
			params.setQueryDatas(medicalRecords);
			respondResult = new RespondResult(true, RespondResult.successCode, null, params);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), params);
		}
		
		return respondResult;
	}
	
	@GetMapping("/coding_defect_page")
	public String codingDefectPage(Model model){
		return "coding/coding_defect_page";
	}
	
	
	@GetMapping("/query_coding_defect")
	@ResponseBody
	public RespondResult queryCodingDefect(StatisticsAnalysisQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecordDefects = new ArrayList<Map<String,Object>>();
			medicalRecordDefects = medicalRecordCodingService.getMedicalRecordOfDefect(params);
				
			respondResult = new RespondResult(true, RespondResult.successCode, null, medicalRecordDefects);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), e);
		}
		
		return respondResult;
	}
	
	
	@GetMapping("/query_coding_defect_detail")
	@ResponseBody
	public RespondResult queryCodingDefectDetail(StatisticsAnalysisQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> defectDetails = new ArrayList<Map<String,Object>>();
			defectDetails = medicalRecordCodingService.getMedicalRecordOfDefectDetail(params);
				
			respondResult = new RespondResult(true, RespondResult.successCode, null, defectDetails);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), e);
		}
		
		return respondResult;
	}
}
